package com.pointi.emoBasedataM;

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.pointi.emoTools.vasUnit;
import com.pointi.emoTools.vasSection;
import com.pointi.emoTools.bdCompareLog;
import com.pointi.emoTools.bdLog;
import com.pointi.emoTools.bdPreProcess;
import com.pointi.emoTools.emoConfig;
import com.pointi.emoTools.emoCheckList;

public class ThreadWork extends Thread
{
	//____________________________________________________________________________________________________
	// DEF
	
	private ConcurrentLinkedQueue<ThreadWork> workThread ;
	private JDBC jdbc ;
	private emoConfig eCONF ; 
	private String callkey ;
	private Date rdate ;
	
	private int start_time ;
	private int start_section ;

	//____________________________________________________________________________________________________
	// INIT
	
	public ThreadWork(ConcurrentLinkedQueue<ThreadWork> workThread)
	{
		this.workThread = workThread ;
	}

	public void start(JDBC jdbc, emoConfig eCONF, String callkey, Date rdate)
	{
		this.jdbc = jdbc ;
		this.eCONF = eCONF ;
		this.callkey = callkey ;
		this.rdate = rdate ;
		workThread.add(this) ;
		super.start() ;
	}
	
	//____________________________________________________________________________________________________
	// RUN // Basedata

	public void run()
	{
		this.fnBasedata_v30() ;
		workThread.remove(this) ;
	}
	
	//____________________________________________________________________________________________________
	// Basedata v3.0
	
	private void fnBasedata_v30()
	{
		//if ((jdbc.fnIsValidCall(_Date, callkey)) > 0) // [T] Start Handling [F] Trash

		//__________________________________________________
		// Pre-Process
		
		this.out("[BD][Pre-Process] Modified Fixed Section") ;
		ArrayList<bdPreProcess> P = new ArrayList<bdPreProcess>() ;
		jdbc.fnUpdateCheckList(rdate, emoCheckList.mUsed, emoCheckList._START, callkey) ;
		jdbc.fnInsertBasedataP(rdate, callkey, eCONF) ;
		jdbc.fnUpdateCrossPoint(rdate, callkey) ;
		jdbc.fnGetBasedataP(rdate, P, callkey) ;

		//__________________________________________________
		// CUT // HT & TD
		
		ArrayList<vasSection> SEC_SUM = new ArrayList<vasSection>() ;
		this.fnConvertSection(P, SEC_SUM) ; // CONVERT SECTION INFO.
		this.fnFindStartTime(SEC_SUM) ;
		int dur = P.size() ;
		int ht = (start_time == 0)? 0 : (start_time - 1) ;
		int td = dur - ht ;
		this.out("[BD][Pre-Process] CUT HT & TD [DUR] " + dur + " [HT] " + ht + " [TD] " + td + " [HT_CNT] " + eCONF.HT_CNT() + " [HT_TYPE] " + eCONF.HT_TYPE()) ;

		//__________________________________________________
		// Build Compare Log
		
		bdCompareLog COMP = new bdCompareLog() ;
		COMP.fnInitialize(Integer.valueOf(callkey)) ;
		jdbc.fnCompareSection(rdate, callkey, COMP) ;
		jdbc.fnInsertBasedataCompare(rdate, COMP) ;
		this.out("[BD] Gathering Low Data ..") ;

		bdLog agent = new bdLog() ;
		bdLog customer = new bdLog() ;
		bdLog full = new bdLog() ;

		agent.fnInitialize(Integer.valueOf(callkey), bdLog.LOG_AGENT, dur, ht, td) ;
		customer.fnInitialize(Integer.valueOf(callkey), bdLog.LOG_CUSTOMER, dur, ht, td) ;
		full.fnInitialize(Integer.valueOf(callkey), bdLog.LOG_FULL, dur, ht, td) ;
		
		jdbc.fnGetBasedataR(rdate, agent, start_time) ;
		jdbc.fnGetBasedataR(rdate, customer, start_time) ;
		jdbc.fnGetBasedataR(rdate, full, start_time) ;
		this.fnBuildBasedataLog(agent, customer, full, SEC_SUM, start_section, td) ;

		jdbc.fnInsertBasedataAgent(rdate, agent.fnGetQuery(jdbc.fnGetYYYYMM(), bdLog.LOG_AGENT));
		jdbc.fnInsertBasedataCustomer(rdate, customer.fnGetQuery(jdbc.fnGetYYYYMM(), bdLog.LOG_CUSTOMER));	
		jdbc.fnInsertBasedataFull(rdate, full.fnGetQuery(jdbc.fnGetYYYYMM(), bdLog.LOG_FULL));	
		this.out("[BD] INSERT Agent / Customer / Full Log") ;

		jdbc.fnUpdateCheckList(rdate, emoCheckList.mUsed, emoCheckList._END, callkey) ;
	}
	
	//____________________________________________________________________________________________________
	// vasResult to vasSection // 2014.06.11
	
	public void fnConvertSection(ArrayList<bdPreProcess> SEC_FIX, ArrayList<vasSection> SEC_SUM)
	{
		int SEC_ID = -1 ;
		int SEC_CNT = -1 ;
		int section_start = 1 ;
		for (int i=0; i<SEC_FIX.size(); i++)
		{
			if (SEC_ID == SEC_FIX.get(i).section()) { SEC_CNT++ ; }
			else
			{
				vasSection SEC_CRR = new vasSection(SEC_FIX.get(0).callkey(), SEC_ID, section_start, SEC_CNT) ;
				if (SEC_ID != -1) { SEC_SUM.add(SEC_CRR) ; section_start += SEC_CNT ; }
				SEC_ID = SEC_FIX.get(i).section() ;
				SEC_CNT = 1 ;
			}
		}
		vasSection SEC_CRR = new vasSection(SEC_FIX.get(0).callkey(), SEC_ID, section_start, SEC_CNT) ;
		SEC_SUM.add(SEC_CRR) ;
	}
	
	//____________________________________________________________________________________________________
	// Find Start Time for HT & TD // 2014.06.11
	
	private void fnFindStartTime(ArrayList<vasSection> SEC_SUM)
	{
		start_time = 0 ;
		start_section = 0 ;
		int cross_cnt = 0 ;
		int prev_section = 0 ;

		for (int i=0; i<SEC_SUM.size(); i++)
		{
			if (SEC_SUM.get(i).section() != 0)
			{
				if (prev_section == 0 || prev_section != SEC_SUM.get(i).section()) 
				{
					prev_section = SEC_SUM.get(i).section() ; 
					cross_cnt++ ;
				}
			}
			if (cross_cnt == eCONF.HT_CNT())
			{
				if (eCONF.HT_TYPE() == emoConfig.HT_TYPE_LESS_THAN)
					start_time = (SEC_SUM.get(i).section_start() + 1) ;
				else if (eCONF.HT_TYPE() == emoConfig.HT_TYPE_NOT_MORE_THAN)
					start_time = SEC_SUM.get(i).section_start() ;
				start_section = i ;
				break ;
			}
		}
		if (eCONF.HT_TYPE() == emoConfig.HT_TYPE_LESS_THAN)
		{
			SEC_SUM.get(start_section).section_start(SEC_SUM.get(start_section).section_start() + 1) ;
			SEC_SUM.get(start_section).section_dur(SEC_SUM.get(start_section).section_dur() - 1) ;
		}
	}
	
	//____________________________________________________________________________________________________
	// Build Basedata Log // Agent Log, Customer Log, Full Log // 2014.06.11 
	
	public void fnBuildBasedataLog(bdLog agent, bdLog customer, bdLog full, ArrayList<vasSection> SEC_SUM, int start_section, int td)
	{
		for (int i=start_section; i<SEC_SUM.size(); i++)
		{
			vasSection SEC_CUR = SEC_SUM.get(i) ;

			if (SEC_CUR.section() == 1) // AGENT
			{
				agent.addtgc() ;
				agent.addtgt(SEC_CUR.section_dur()) ;
				customer.addmc(SEC_CUR.section_dur()) ;
				customer.addmt(SEC_CUR.section_dur()) ;
				full.addtgc() ;
				full.addtgt(SEC_CUR.section_dur()) ;
			}
			else if (SEC_CUR.section() == 2) // CUSTOMER
			{
				customer.addtgc() ;
				customer.addtgt(SEC_CUR.section_dur()) ;
				agent.addmc(SEC_CUR.section_dur()) ;
				agent.addmt(SEC_CUR.section_dur()) ;
				full.addtgc() ;
				full.addtgt(SEC_CUR.section_dur()) ;
			}
			else // MUTE
			{
				agent.addmc(SEC_CUR.section_dur()) ;
				agent.addmt(SEC_CUR.section_dur()) ;
				customer.addmc(SEC_CUR.section_dur()) ;
				customer.addmt(SEC_CUR.section_dur()) ;
				full.addmc(SEC_CUR.section_dur()) ;
				full.addmt(SEC_CUR.section_dur()) ;
			}
		}
		agent.setTD(td) ;
		customer.setTD(td) ;
		full.setTD(td) ;
	}
	
	//____________________________________________________________________________________________________
	// Valid Anger Index // 2014.06.11
	
	public synchronized void fnValidAngerIndex(ArrayList<vasUnit> inUnit, ArrayList<int[]>inIndex)
	{
		for (int i=0; i<inUnit.size(); i++)
		{
			if (inIndex.get(i)[1] == 0 && inUnit.get(i).nonvoice_cnt() != 50)
			{
				inUnit.get(i).voice1_cnt(0) ;
				inUnit.get(i).voice2_cnt(0) ;
				inUnit.get(i).nonvoice_cnt(50) ;
				inUnit.get(i).voice1_idx(0) ;
				inUnit.get(i).voice2_idx(0) ;
				inUnit.get(i).nonvoice_idx(100) ;
			}
		}
	}
	
	private void out(Object obj)
	{
		System.out.println(obj) ;
	}
	
	//____________________________________________________________________________________________________
	// EOC
}
